<template>
  <div class="picture-search-container">
    <!-- 搜索表单卡片 -->
    <div class="picture-search-card">
      <div class="search-title">图片搜索</div>
      <a-form
        name="searchForm"
        layout="inline"
        :model="searchParams"
        @finish="doSearch"
        class="search-form"
      >
        <a-form-item label="关键词" name="searchText" class="form-item">
          <a-input
            v-model:value="searchParams.searchText"
            placeholder="从名称和简介搜索"
            allow-clear
            class="form-input"
          />
        </a-form-item>

        <a-form-item name="category" label="分类" class="form-item">
          <a-auto-complete
            v-model:value="searchParams.category"
            style="min-width: 180px"
            placeholder="请输入分类"
            :options="categoryOptions"
            allow-clear
            class="form-input"
          />
        </a-form-item>

        <a-form-item name="tags" label="标签" class="form-item">
          <a-select
            v-model:value="searchParams.tags"
            style="min-width: 180px"
            mode="tags"
            placeholder="请输入标签"
            :options="tagOptions"
            allow-clear
            class="form-input"
          />
        </a-form-item>

        <!-- 日期范围项 - 调整为与其他项在同一行 -->
        <a-form-item label="日期" name="dateRange" class="form-item date-range-item">
          <a-range-picker
            v-model:value="dateRange"
            :placeholder="['开始时间', '结束时间']"
            format="YYYY/MM/DD HH:mm:ss"
            :presets="rangePresets"
            @change="onRangeChange"
            class="form-input"
          />
        </a-form-item>

        <a-form-item label="名称" name="name" class="form-item">
          <a-input
            v-model:value="searchParams.name"
            placeholder="请输入名称"
            allow-clear
            class="form-input"
          />
        </a-form-item>

        <a-form-item label="简介" name="introduction" class="form-item">
          <a-input
            v-model:value="searchParams.introduction"
            placeholder="请输入简介"
            allow-clear
            class="form-input"
          />
        </a-form-item>

        <a-form-item label="宽度" name="picWidth" class="form-item">
          <a-input-number
            v-model:value="searchParams.picWidth"
            class="form-input"
          />
        </a-form-item>

        <a-form-item label="高度" name="picHeight" class="form-item">
          <a-input-number
            v-model:value="searchParams.picHeight"
            class="form-input"
          />
        </a-form-item>

        <a-form-item label="格式" name="picFormat" class="form-item">
          <a-select
            v-model:value="searchParams.picFormat"
            placeholder="请选择格式"
            allow-clear
            class="form-input"
          >
            <a-select-option value="png">PNG</a-select-option>
            <a-select-option value="jpg">JPG</a-select-option>
            <a-select-option value="jpeg">JPEG</a-select-option>
            <a-select-option value="webp">WebP</a-select-option>
            <a-select-option value="gif">GIF</a-select-option>
            <a-select-option value="bmp">BMP</a-select-option>
            <a-select-option value="svg">SVG</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item class="form-item action-buttons">
          <a-space>
            <a-button
              type="primary"
              html-type="submit"
              style="width: 96px"
              class="search-button"
            >
              <template #icon><SearchOutlined /></template>
              搜索
            </a-button>
            <a-button
              html-type="reset"
              @click="doClear"
              class="reset-button"
            >
              <template #icon><SyncOutlined /></template>
              重置
            </a-button>
          </a-space>
        </a-form-item>
      </a-form>
    </div>

  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import dayjs from 'dayjs'
import { SearchOutlined, SyncOutlined } from '@ant-design/icons-vue'
import {listPictureTagCategoryUsingGet} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'

interface Props {
  onSearch?: (searchParams: API.PictureQueryRequest) => void
}

const props = defineProps<Props>()

// 搜索条件
const searchParams = reactive<API.PictureQueryRequest>({})

// 搜索数据
const doSearch = () => {
  props.onSearch?.(searchParams)
}

// 标签和分类选项
const categoryOptions = ref<string[]>([])
const tagOptions = ref<string[]>([])

/**
 * 获取标签和分类选项
 */
const getTagCategoryOptions = async () => {
  try {
    const res = await listPictureTagCategoryUsingGet()
    if (res.data.code === 0 && res.data.data) {
      tagOptions.value = (res.data.data.tagList ?? []).map((data: string) => ({
        value: data,
        label: data,
      }))
      categoryOptions.value = (res.data.data.categoryList ?? []).map((data: string) => ({
        value: data,
        label: data,
      }))
    } else {
      message.error('获取标签分类列表失败：' + (res.data.message || '未知错误'))
    }
  } catch (error) {
    message.error('获取标签分类列表时发生错误')
    console.error(error)
  }
}

onMounted(() => {
  getTagCategoryOptions()
})

const dateRange = ref<[]>([])

/**
 * 日期范围更改时触发
 * @param dates
 * @param dateStrings
 */
const onRangeChange = (dates: any[], dateStrings: string[]) => {
  if (dates?.length >= 2) {
    searchParams.startEditTime = dates[0].toDate()
    searchParams.endEditTime = dates[1].toDate()
  } else {
    searchParams.startEditTime = undefined
    searchParams.endEditTime = undefined
  }
}

// 时间范围预设
const rangePresets = ref([
  { label: '过去 7 天', value: [dayjs().add(-7, 'd'), dayjs()] },
  { label: '过去 14 天', value: [dayjs().add(-14, 'd'), dayjs()] },
  { label: '过去 30 天', value: [dayjs().add(-30, 'd'), dayjs()] },
  { label: '过去 90 天', value: [dayjs().add(-90, 'd'), dayjs()] },
])

// 清理
const doClear = () => {
  // 清空所有搜索参数
  Object.keys(searchParams).forEach((key) => {
    searchParams[key] = undefined
  })
  // 清空日期范围
  dateRange.value = []
  // 触发搜索
  props.onSearch?.(searchParams)
}

</script>

<style scoped>
.picture-search-container {
  padding: 20px;
}

.picture-search-card {
  background: #fff;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
}

.picture-search-card:hover {
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.search-title {
  font-size: 18px;
  font-weight: 600;
  color: #165DFF;
  margin-bottom: 20px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: flex-end; /* 对齐所有表单项底部 */
}

.form-item {
  margin-bottom: 16px !important;
  transition: all 0.2s ease;
  flex: none; /* 取消自动伸缩 */
}

.form-item:hover {
  transform: translateY(-2px);
}

.form-input {
  width: 100% !important; /* 让输入框充满表单项宽度 */
  min-width: 270px !important; /* 设置最小宽度 */
  transition: all 0.3s ease !important;
}

/* 日期范围项特殊处理 */
.date-range-item {
  /* 给予适当宽度但不独占一行 */
  min-width: 300px !important;
  width: auto !important;
}

/* 日期选择器调整 */
.date-range-item .form-input {
  width: 100% !important;
}

.form-input:focus-within {
  box-shadow: 0 0 0 2px rgba(22, 93, 255, 0.2) !important;
  border-color: #165DFF !important;
}

.action-buttons {
  margin-left: auto !important;
  padding-left: 16px;
}

.search-button {
  background: #165DFF !important;
  border-color: #165DFF !important;
  transition: all 0.3s ease !important;
}

.search-button:hover {
  background: #0E42D2 !important;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(22, 93, 255, 0.3) !important;
}

.search-button:active {
  transform: translateY(0);
}

.reset-button {
  transition: all 0.3s ease !important;
}

.reset-button:hover {
  color: #165DFF !important;
  border-color: #165DFF !important;
  transform: translateY(-2px);
}

.reset-button:active {
  transform: translateY(0);
}

/* 响应式调整 */
@media (max-width: 1200px) {
  .date-range-item {
    min-width: 250px !important;
  }
}

@media (max-width: 768px) {
  .picture-search-card {
    padding: 16px;
  }

  .search-form {
    flex-direction: column;
    gap: 8px;
    align-items: stretch; /* 移动端纵向排列 */
  }

  .form-item, .date-range-item {
    width: 100% !important;
    min-width: auto !important;
  }

  .action-buttons {
    margin-left: 0 !important;
    padding-left: 0;
    align-self: flex-start;
  }
}
</style>
